import Vue from 'vue'
import Router from 'vue-router'
import main2 from '@/view/main2';
import screening2 from '@/components/screening2';
import result2 from '@/components/result2';
import detail2 from '@/view/detail2';
import login2 from '@/view/login2';
import admin from '@/view/admin'

Vue.use(Router)

const router = new Router({
    routes: [
        {
            component: main2,
            path: '/',
            name: 'main2',
            redirect: '/login2',
            children: [
                {
                    path: 'login2',
                    component: login2,
                    name: 'login2'
                },
                {
                    path: 'screening2',
                    component: screening2,
                    name: 'screening2',
                    meta: {
                        requireAuth: true
                    }
                },
                {
                    path: 'result2',
                    component: result2,
                    name: 'result2',
                    meta: {
                        requireAuth: true
                    }
                },
                {
                    path: 'admin',
                    component: admin,
                    name: 'admin',
                    meta: {
                        requireAuth: true
                    }
                }
            ]
        },
        {
            component: detail2,
            path: '/detail2/:id',
            name: 'detail2'
        }
    ]
})


router.beforeEach((to, from, next) => {
    if (to.meta.requireAuth) {
        if (window.localStorage.getItem('screeningLogin')) {
            next();
        } else {
            next({
                path: '/login2',
                query: {redirect: to.fullPath}
            })
        }
    } else {
        next()
    }
})

export default router;
